using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class slingshoter : MonoBehaviour
{
    public static slingshoter Instance { get; private set; } 
    private LineRenderer left;
    private LineRenderer right;

    private Transform leftp;
    private Transform rightp;
    private Transform midp;

    private bool isdraw;
    private Transform bird;

    private void Awake()
    {
        Instance = this;
        left = transform.Find("left").GetComponent<LineRenderer>();
        right = transform.Find("right").GetComponent<LineRenderer>();
        leftp = transform.Find("leftpoint");
        rightp = transform.Find("rightpoint");
        midp = transform.Find("centerpoint");
    }
    // Start is called before the first frame update
    void Start()
    {
        
        Hide();
    }

    // Update is called once per frame
    void Update()
    {
        if (isdraw)
        {
            Draw();
        }
    }
    public void StartDraw(Transform bird)
    {
        isdraw=true;
        this.bird= bird;
        Show();
    }
    public void EndDraw()
    {
        isdraw =false;
        Hide();
    }
    public void Draw()
    {
        Vector3 birdp= bird.position;
        Vector3 bp=(birdp - midp.position.normalized )* 0.03f+birdp;

        left.SetPosition(0,bp);
        left.SetPosition(1,leftp.position);

        right.SetPosition(0, bp);
        right.SetPosition(1,rightp.position);
    }
    public Vector3 getcenter()
    {
        return midp.position;
    }
    private void Hide()
    {
        left.enabled = false;
        right.enabled = false;
    }
    private void Show()
    {
        left.enabled=true;
        right.enabled=true; 
    }
    
        
}
